* regressions_formal_bite_industry_ccity.do	JEP	04/11/15
* Does regressions that are in the term paper, for formal workers: RIF4
* Monthly wages
* Exclude some industries
* Do regressions by city - industry
* Cluster by city, so CGM

*************************** Change log ********************************** 

/*
	
*/	

clear 
version 13.1

* Project info
cap project, doinfo
if _rc==198 {
	if c(os)=="Unix" loc master "/home/jperez/AA_Minimum_Wage" 
 else if c(username)=="J16339" | c(username)=="A15858" loc master "B:\Col_Minimum_Wage"  
 	else loc master "C:/Users/jorpp/Dropbox (Brown)/Col_Minimum_Wage"
 loc pr = 0
}
else {
	local master "`r(pdir)'"
	local doname "`r(dofile)'"  
 loc pr = 1
	* Project calls
	project, uses("`master'/Data/Source/ocupados_mw.dta")
	* Call programs
	project, relies_on("`master'/Code/Est/sample_choice.do")
	project, relies_on("`master'/Code/Est/build_mw_measures.do")
	project, relies_on("`master'/Code/Est/build_treatment.do")
	project, relies_on("`master'/Code/Est/export_rif.do")
	project, relies_on("`master'/Code/Est/confirmedrun.do")
	project, relies_on("`master'/Code/Est/graph_rif.do")
}
*/


* Call necessary programs. I do not call using project to carry the programs around. I do not put these in the ado/personal folder because they are specific to the project. They are not ado files.
do "`master'/Code/Est/sample_choice.do"
do "`master'/Code/Est/build_mw_measures.do"
do "`master'/Code/Est/build_treatment.do"
do "`master'/Code/Est/export_rif.do"
do "`master'/Code/Est/confirmedrun.do"
do "`master'/Code/Est/graph_rif.do"


use "`master'/Data/Source/ocupados_mw.dta", clear

* Choose quantiles
glo qtiles "20	21	22	23	24	25	26	27	28	29	30	31	32	33	34	35	36	37	38	39	40	41	42	43	44	45	46	47	48	49	50	51	52	53	54	55	56	57	58	59	60	61	62	63	64	65	66	67	68	69	70	71	72	73	74	75	76	77	78	79	80"
* glo qtiles "5"
* Number of bootstraps
glo boot=1

* RIF 4 - Tables numbered 4 in term paper

* Choose sample
mysample rif4sample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) formal exind(13,29,42)
tab rif4sample
tab tipo_trabajador if rif4sample
tab year if rif4sample
tab horas_semana if rif4sample 
tab sex if rif4sample
tab afiliado_salud if rif4sample
* Build mw measures
mw_measures rif4sample , stub(rif4)
* Build treatment
mytreatment, posts("tq(1999q1)") mw(bite_rif4_ind) wage(salario_mensual_real)

* RIF regressions

* Create estimation results folder if it doesn't already exist
cap mkdir "`master'/Ster"
cap mkdir "`master'/Ster/rif4_new_indcc_peak"

* For these I'm clustering within city, full CGM again

foreach x in $qtiles {
	di " Percentile `x'"
	loc fw: word 1 of $qtiles
	loc count: word count $qtiles
	di `count'
	loc lw: word `count' of $qtiles
	if `x'==`lw' & `pr'!=0 loc reg="register"
	else loc reg ""
	if `x'==`fw' loc r="replace"
	else loc r="append"
	
	
	* Panel A. Employment
	*confirmedrun, `reg' controls() name(`master'/Ster/rif4_new_indcc_peak/reg_emp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc) rif(rif4A) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode)
	* cgmrif2 lw treat i.ccode i.time emp if rif4sample , hyp(0) boot($boot) cluster(ccode) x(treat) i(ccode) t(time) q(`x') rif(rif4A`x')
	* exportrif, name(`master'/Ster/rif4_new/reg_emp_tq(1999q1)_bite_salario_mensual_real_50_edom) p(`x') r(`r')
	* Panel B. Employment and Bartik Price Variable
	confirmedrun, `reg' controls(bartikp1_ex) name(`master'/Ster/rif4_new_indcc_peak/reg_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc) rif(rif4B) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) rerun 
	* Panel C. Employment and Bartik Quantity Variable
	*confirmedrun, `reg' controls(bartikq_ex) name(`master'/Ster/rif4_new_indcc_peak/reg_emp_bq_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc) rif(rif4C) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode)
	* Panel D. Employment and City Trends
	*confirmedrun, `reg' controls(i.ccode#c.trend) name(`master'/Ster/rif4_new_indcc_peak/reg_sst_emp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc) rif(rif4D) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) 
	* Panel E. Employment, Bartik Price Variable and City Trends
	confirmedrun, `reg' controls(i.ccode#c.trend bartikp1_ex) name(`master'/Ster/rif4_new_indcc_peak/reg_sst_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc) rif(rif4E) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) rerun 
	* Panel F. Employment, Bartik Quantity Variable and City Trends
	*confirmedrun, `reg' controls(i.ccode#c.trend bartikq_ex) name(`master'/Ster/rif4_new_indcc_peak/reg_sst_emp_bq_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc) rif(rif4F) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) 
	
	
	/*
	Con city ind FE , estas son igual a las anteriores
	* Panel G. Employment, Bartik Price Variable - Industry fe, city fe and interactions
	confirmedrun, `reg' controls(bartikp1_ex) name(`master'/Ster/rif4_new_indcc_peak/reg_emp_bp_ixc_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc) rif(rif4G) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode)
	* Panel H. Employment, Bartik Quantity Variable and City - Industry fe, city fe and interactions
	confirmedrun, `reg' controls(bartikq_ex) name(`master'/Ster/rif4_new_indcc_peak/reg_emp_bq_ixc_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc) rif(rif4H) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode)
	*/
	/*
	* Panel I - Employment, Bartik Price Variable - Industry fe, city fe and interactions - City Industry trends
	confirmedrun, `reg' controls(bartikp1_ex) name(`master'/Ster/rif4_new_indcc_peak/reg_ct_emp_bp_ixc_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc) rif(rif4I) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.ccode#i.cactividad_empresa#c.time i.time) cluster(ccode)
	* Panel J. Employment, Bartik Quantity Variable and City - Industry fe, city fe and interactions
	confirmedrun, `reg' controls(bartikq_ex) name(`master'/Ster/rif4_new_indcc_peak/reg_ct_emp_bq_ixc_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc) rif(rif4J) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.ccode#i.cactividad_empresa#c.time i.time) cluster(ccode) rerun
	*/
	* Panel K. Employment, Bartik Price Variable and Industry Trends
	*confirmedrun, `reg' controls(i.cactividad_empresa#c.trend bartikp1_ex) name(`master'/Ster/rif4_new_indcc_peak/reg_it_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc) rif(rif4K) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode)
	* Panel L. Employment, Bartik Price Variable and City Trends and Industry Trends
	*confirmedrun, `reg' controls(i.ccode#c.trend i.cactividad_empresa#c.trend bartikp1_ex) name(`master'/Ster/rif4_new_indcc_peak/reg_itct_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc) rif(rif4L) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode)
	* Panel M. Employment, Bartik Price Variable and Industry Trend post crisis
	*confirmedrun, `reg' controls(i.cactividad_empresa#c.trend#0.post i.cactividad_empresa#c.trend#1.post bartikp1_ex) name(`master'/Ster/rif4_new_indcc_peak/reg_itp_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc) rif(rif4M) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) rerun
	* Panel N. Employment, Bartik Price Variable and national employment by industry
	confirmedrun, `reg' controls(empind bartikp1_ex) name(`master'/Ster/rif4_new_indcc_peak/reg_ie_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc) rif(rif4N) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) rerun 
	* Panel O. Employment, Bartik Price Variable and national employment by industry, city trends
	confirmedrun, `reg' controls(empind bartikp1_ex i.ccode#c.trend) name(`master'/Ster/rif4_new_indcc_peak/reg_iect_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc) rif(rif4O) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) rerun 
		
} 

* Graph results and export

clear

cap program drop mex
program define mex
	cap mkdir "`2'/Doc/ster_figures/rif4_new_indcc_peak"
	cap mkdir "`2'/Doc/ster_figures/rif4_new_indcc_peak/1999q1"
	graph export "`2'/Doc/ster_figures/rif4_new_indcc_peak/1999q1/`1'.pdf", replace
	*! epstopdf "`2'/Doc/ster_figures/rif4_new_indcc_peak/1999q1/`1'.eps"
	* erase "`2'/Doc/ster_figures/rif4_new_indcc_peak/1999q1/`1'.eps"
	*project, creates("`2'/Doc/ster_figures/rif4_new_indcc_peak/1999q1/`1'.pdf")
end	

* loc master "C:/Users/jorpp/Dropbox (Brown)/Col_Minimum_Wage"

loc master "B:\Col_Minimum_Wage"  
*foreach spec in reg_emp reg_emp_bp reg_emp_bq reg_sst_emp_bp reg_sst_emp_bq reg_emp reg_emp_bp reg_emp_bq reg_sst_emp_bp reg_sst_emp_bq reg_emp_bq_ixc reg_emp_bp_ixc reg_ct_emp_bp_ixc reg_ct_emp_bq_ixc	{
foreach spec in reg_emp_bp reg_sst_emp_bp reg_ie_emp_bp reg_iect_emp_bp {
	di "`spec'"
	graph drop _all
	graphrif, master("`master'") ster(Ster) folder(rif4_new_indcc_peak) qtiles($qtiles) spec(`spec') time(tq(1999q1)) rareaopts(bcolor(gs13))twowayopts(title("Formal sector - Bite - By city industry" "`spec' - 1999q1") xtitle(Percentile) ytitle(Coef) yscale(range(-0.0075 0.0075)) ylabel(-0.0075(0.0025)0.0075) yline(0) caption(Bootstrapped s.e. clustered by city)  )
	mex `spec' "`master'"
}




